#include "Textbox.h"

Textbox::Textbox(int posX, int posY, Widget *parent) :
		Widget(posX, posY, parent) {

	this->font = TTF_OpenFont("./trunk/res/font/Neuton.ttf", 18);

	if (this->font == NULL) {
		Logger::log("Textbox", "Textbox", "No se pudo inicializar la fuente");
	}

	this->textboxSurface = loadSurface("./trunk/res/img/textbox.png");

	this->parcel = this->getPosition();
	this->parcel.height = this->textboxSurface->h;
	this->parcel.width = this->textboxSurface->w;

	defaultText = TTF_RenderText_Solid(font, "Ingrese nombre...", textColor);

	activeText = defaultText;

	SDL_EnableUNICODE(SDL_ENABLE);

	this->input = "";
	this->font = font;
	this->textColor.b = 0;
	this->textColor.g = 0;
	this->textColor.r = 0;
	this->text = NULL;
	this->hasFocus = false;
	this->inputChanged = false;
	this->mousePressed = false;
	this->mouseIsOver = false;
	this->keyDown = false;
	this->isChar = false;
	this->backSpace = false;

	keyState = 0;
	keyPressed = false;

}

Textbox::~Textbox() {

	SDL_FreeSurface(text);
	SDL_FreeSurface(defaultText);
	SDL_FreeSurface(textboxSurface);
	SDL_EnableUNICODE(SDL_DISABLE);
	TTF_CloseFont(font);

}

bool Textbox::getFocus() const {
	return this->hasFocus;
}

void Textbox::setFocus() {
	this->hasFocus = true;
}

void Textbox::unsetFocus() {
	this->hasFocus = false;
}

void Textbox::handleEvent(Event event) {
	mousePressed = (event.getButtonState() == Pressed);
	mouseIsOver = mouseOver(event);
	keyDown = (event.getType() == KeyPressed);
	isChar = ((event.getKeyUnicode() >= (Uint16) 32)
			&& (event.getKeyUnicode() <= (Uint16) 255));
	tempInput = (char) event.getKeyUnicode();
	backSpace = (event.getKey() == Backspace);

	keyState = event.getType();
}

void Textbox::resolve() {

	if (this->mousePressed && (!this->mouseIsOver) && this->hasFocus) {
		this->unsetFocus();
	} else if (this->mousePressed && this->mouseIsOver) {
		this->setFocus();
	}

	if (hasFocus && keyState == KeyPressed) {
		keyPressed = true;
		if (isChar || backSpace) {
			inputChanged = true;
			letra = tempInput;
		}
	}
	if (hasFocus && keyState == KeyReleased && keyPressed) {
		keyPressed = false;

		if (inputChanged) {
			if (backSpace) {
				if (input.length() > 0) {
					input.erase(input.length() - 1);
				}else {
					inputChanged = false;
				}

			} else {
				input += letra;
			}
		}
	}

	if ((!hasFocus) && (input.length() == 0)) {
		activeText = defaultText;
	} else {
		if (inputChanged || input.length() == 0) {
			std::string aux;
			if (this->input.length() >= MAX_SIZE) {
				aux = this->input.substr((this->input.length() - MAX_SIZE),
				MAX_SIZE);
			} else {
				aux = this->input.substr(0, MAX_SIZE);
			}

			char* c_input = new char[MAX_SIZE + 1];strcpy
			(c_input, aux.c_str());

			SDL_FreeSurface(text);
			text = TTF_RenderText_Solid(this->font, c_input, this->textColor);
			activeText = text;

			delete[] c_input;
		}
	}
}

void Textbox::showOn(SDL_Surface* surface) {

	SDL_Rect rectBox;
	rectBox.x = parcel.posX;
	rectBox.y = parcel.posY;
	rectBox.w = parcel.width;
	rectBox.h = parcel.height;

	SDL_BlitSurface(this->textboxSurface, NULL, surface, &rectBox);

	SDL_Rect rect;
	rect.x = this->parcel.posX;
	rect.y = this->parcel.posY;
	SDL_BlitSurface(activeText, NULL, surface, &rect);

}

std::string Textbox::getInput() {
	return this->input;
}
